import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/Home.vue'
import Category from "../views/category/Category";

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      activeNum: 0,
      isShowTabBar: true
    },
    children: [{path: '/home/popup',component: () => import('../views/home/SearchPopup')}]
  },
  {
    path: '/topic',
    meta: {
      activeNum: 1,
      isShowTabBar: true
    },
    component: () => import('../views/topic/Topic')
  },
  {
    path: '/category',
    meta: {
      activeNum: 2,
      isShowTabBar: true
    },
    component: () => import('../views/category/Category'),
    children: [{
        path: '/category/popup',
        component: () => import('../views/home/SearchPopup')
    }]
  },
  {
    path: '/cart',
    meta: {
      activeNum: 3,
      isShowTabBar: true
    },
    component: () => import('../views/cart/Cart')
  },
  {
    path: '/user',
    meta: {
      activeNum: 4,
      isShowTabBar: true
    },
    component: () => import('../views/user/User')
  },
  {
      path: '/pruductDetail',
      component: () => import('../views/pruductDetail/PruductDetail')
  },
  {
      path: '/channel',
      component: () => import('../views/home/Channel')
  },
  {
      path: '/brand',
      component: () => import('../views/home/Brand')
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to,from,next)=> {
  const token = sessionStorage.getItem('token')
  if(to.path == '/cart') {
    if(token) {
      next()
    }else {
      Vue.prototype.$toast('请先登录')
      next('/user')
    }
  }
  next()
})

export default router
